/*******************************************************************************
File: 3_cpfp_involvement
Project: CPKIN
Purpose: This .do file cleans and analyzes data on involvement of CPFPs in child protection activities 
Author: Akshay Dixit
Date Created: Feb 1, 2019
Date modified: June 28, 2020
********************************************************************************/

clear all
set more off

***THIS IS THE INPUT THAT MUST BE CHANGED SPECIFIC TO THE USER***
*gl identity ""
*gl user "" 

********************************************************************************

***Creating Global File Paths***'
gl data "$user/data"
gl dof "$user/do files"
gl analysis "$user/analysis"

********************************************************************************

u "$data/endline_adult_chief_child_data.dta", clear

* Dropping child survey - these questions were not asked of children at all
drop if type_survey == "child"

* Stratification variable - Chiefdom
gl chiefdom = "bagruwa_chief barri_chief bumpeh_chief fakunya_chief gallinas_perri_chief kaiyamba_chief kamarjei_chief kori_chief kowa_chief kpaka_chief malin_chief sowa_chief"

* Control variables
gl controls = "wealth_index female age attend_school fp_visit vil_pop"

* Create binary versions of the outcome variables
desc fp_pnsh_hvywork fp_sev_beat fp_care fp_med fp_study fp_needs fp_preg fp_sch 
foreach var in fp_pnsh_hvywork fp_sev_beat fp_care {
	di "`var'"
	replace `var' = 0 if know_fp == 0
	tab `var'
	tab treatment, sum(`var')
		
	count if (`var' == . | `var' == .d | `var' == .c)
	count if `var' == .	
	count if `var' == .d		//If var = 4, i.e. "This incident has not happened in this village in the past 6 months"
								//fp_pnsh_hvywork: 420 .d responses
								//fp_sev_beat: 414 .d responses
								//fp_care: 398 .d responses
								
	count if `var' == .c		//If var = 777, i.e. "Does not exist in the village"
	g `var'_binary = (`var' == 1 | `var' == 2 | `var' == 3)
	replace `var'_binary = . if (`var' == .)	//.d and .c coded as 0
}


foreach var in fp_med fp_study fp_needs fp_preg fp_sch {
	replace `var' = 0 if know_fp == 0
	tab `var'
	
	count if (`var' == . | `var' == .d | `var' == .c)
	count if `var' == .
	count if `var' == .d
	count if `var' == .c
	g `var'_binary = (`var' == 1 | `var' == 2 | `var' == 3)
	replace `var'_binary = . if (`var' == .)	//.d and .c coded as 0
	tab `var'
}

	
* Label variables
lab var fp_pnsh_hvywork_binary "Punishing adult for forcing child to do heavy work"
lab var fp_sev_beat_binary "Punishing adult for severely beating child"
lab var fp_care_binary "Providing care and protection for child severely beaten"
lab var fp_med_binary "Helping children get medical care/medicine"
lab var fp_study_binary "Providing encouragement to study"
lab var fp_needs_binary "Providing assistance to children for their basic needs"
lab var fp_preg_binary "Helping pregnant minors get medical care"
lab var fp_sch_binary "Helping out of school children return to school"

lab var treatment "Treatment"
lab var wealth_index "Wealth index" 
lab var female "Female"
lab var age "Age"
lab var attend_school "Ever attended school" 
lab var fp_visit "Village visited by UNICEF monitoring team"
lab var vil_pop "Village population"
lab var chief "Respondent: Chief"
lab var sms_received "CPKIN SMS received"

********************************************************************************

***ITT analysis***

cd "$analysis"

cap erase "ITT_cpfp_involvement.xls"

local outcomes fp_pnsh_hvywork_binary fp_sev_beat_binary fp_care_binary fp_med_binary fp_study_binary fp_needs_binary fp_preg_binary fp_sch_binary
foreach var of local outcomes {
	
	di "`var'" 

	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	
	qui reg `var' treatment $chiefdom $controls chief, vce(cluster village_id1)
	
	local p = 2 * (1-normal(abs(_b[treatment]/_se[treatment])))
	local p_round = round(`p', 0.001)
	
	outreg2 using "ITT_cpfp_involvement.xls", append label keep(treatment wealth_index female age attend_school fp_visit vil_pop chief) ///
	addtext(Naive p-value, "`p_round'", Control group mean, "`rounded'") ///
	addnote("Standard errors clustered at the village-level. Specifications include binary variables for randomization strata.")
	
}

cap erase "ITT_cpfp_involvement.txt"

********************************************************************************

***TOT analysis with sms_received as instrument***

cap erase "TOT_cpfp_involvement.xls"	
	
local outcomes fp_pnsh_hvywork_binary fp_sev_beat_binary fp_care_binary fp_med_binary fp_study_binary fp_needs_binary fp_preg_binary fp_sch_binary
foreach var of local outcomes {
	
	di "`var'"
	
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	
	qui ivregress 2sls `var' $chiefdom $controls chief (sms_received = treatment), vce(cluster village_id1)
	
	local p = 2 * (1-normal(abs(_b[sms_received]/_se[sms_received])))
	local p_round = round(`p', 0.001)
	
	outreg2 using "TOT_cpfp_involvement.xls", append label keep(sms_received wealth_index female age attend_school fp_visit vil_pop chief) ///
	addtext(Naive p-value, "`p_round'", Control group mean, "`rounded'") ///
	addnote("Standard errors clustered at the village-level. Specifications include binary variables for randomization strata.")
	
}

cap erase "TOT_cpfp_involvement.txt"

********************************************************************************

clear


